package com.bwie.user.controller;

import java.util.*;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.common.utils.Md5Utils;
import com.bwie.user.feign.BargainFeign;
import com.bwie.user.util.OssUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 用户表
 *
 * @author lfw
 * @email sunlightcs@gmail.com
 * @date 2022-02-21 20:57:16
 */
@RestController
@RequestMapping("user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    BargainFeign bargainFeign;



    /**
     * 文件上传
     */
    @RequestMapping("/upload")
    public R register(MultipartFile file){
        OssUtils ossUtils = new OssUtils();
        R r = ossUtils.uploadFile(file);
        return r;
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity user){
        /*String user1 = (String) params.get("user");
        String bargain = (String) params.get("bargain");*/
        String s = Md5Utils.md5(user.getPassword() + "lfw");
        String pass = Md5Utils.md5(s + "lfw");
        UserEntity one = userService.getOne(new QueryWrapper<UserEntity>().eq("nickname", user.getNickname())
                .eq("password", pass));

        if (StringUtils.isNotBlank(user.getUser()) && StringUtils.isNotBlank(user.getBargain()) && one!=null ){
            if (user.getUser().equals(one.getId()+""))
            return R.error("不能自己帮自己砍");

            Map<String, Object> map = Collections.synchronizedMap(new HashMap<String, Object>(16));
            //HashMap<String, Object> map = new HashMap<>();
            map.put("user", user.getUser());
            map.put("bargain", user.getBargain());
            map.put("help", one.getId()+"");
            return bargainFeign.helpbargain(map);
        }
        if (one!=null){
            return R.ok().put("user", one.getId());
        }
        return R.error();
    }


        /**
     * 注册
     */
    @RequestMapping("/register")
    public R register(@RequestBody UserEntity user){
        //设置盐为固定值
        user.setSalt("lfw");
        //密码和盐加密 第一次加密
        String md5 = Md5Utils.md5(user.getPassword() + user.getSalt());
        //md5加盐 二次加密形成密码
        String pass = Md5Utils.md5(md5 + user.getSalt());
        user.setPassword(pass);
        user.setRegisterData(new Date());
        userService.save(user);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);
        System.out.println(4 / 0);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
